package com.plant.user.utils;

import com.plant.user.enumm.ErrorCode;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class AddUserUtils {
    public boolean isValidPassword(String password, String value1) {
        if (password == null || password.isEmpty()) {
            return false;
        }
        Boolean chinese = ChinesePattern.isChinese(password);
        if (!password.equals(value1)){
            return false;
        }
        return!chinese && password.length() >= 8 && password.length() <= 32 && password.equals(value1);
    }

    public ErrorCode getPasswordErrorResultCode(String password,String value1) {
        if (password == null || password.isEmpty()) {
            return ErrorCode.PASSWORD_CANNOT_BE_EMPTY;
        } else if (ChinesePattern.isChinese(password)) {
            return ErrorCode.PASSWORD_CONTAINS_CHINESE;
        } else if (password.length() < 8 || password.length() > 32) {
            return ErrorCode.PASSWORD_LENGTH_INVALID;
        } else if (!password.equals(value1)){
            return ErrorCode.PASSWORD_NOT_MATCH;
        }
        return ErrorCode.SUCCTRESS;
    }

    public String getPasswordErrorMessage(String password) {
        if (password == null || password.isEmpty()) {
            return "密码不可为空";
        } else if (ChinesePattern.isChinese(password)) {
            return "密码不可包含汉字";
        } else if (password.length() < 8 || password.length() > 32) {
            return "密码长度必须在 8 - 32 之间";
        } else {
            return "两次密码不一致";
        }
    }

    public String generateRandomUsername(int length) {
        StringBuilder username = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 生成随机的字母或数字字符
            int choice = random.nextInt(2);
            if (choice == 0) {
                username.append((char) (random.nextInt(26) + 'a'));
            } else {
                username.append(random.nextInt(10));
            }
        }
        return username.toString();
    }


    public boolean ValidPassword(String password) {
        if (password == null || password.isEmpty()) {
            return false;
        }
        Boolean chinese = ChinesePattern.isChinese(password);
        return!chinese && password.length() >= 8 && password.length() <= 32;
    }


}
